package Servlet;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import dao.StudentDAO;
import entity.Student;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author smile
 */
public class AddStudents extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String id=request.getParameter("StudentID");
        String name=request.getParameter("Name");
        String age=request.getParameter("Age");
        String Address=request.getParameter("Address");
        String Gender=request.getParameter("Gender");
        String Image=request.getParameter("Image");
        String Batch=request.getParameter("Batch");
        boolean done=true;
        if(id==null || id.isEmpty())
        {
            out.println("<br>Roll Number cannot be blank</br>");
            done=false;
        }
        if(name==null || name.isEmpty())
        {
            out.println("<br>You must enter Student Name</br>");
            done=false;
        }
        if(age==null || age.isEmpty())
        {
            out.println("<br>Age cannot be blank</br>");
            done=false;
        }
        if(Integer.parseInt(age)<0)
        {
            out.println("<br>Age,must be a number and >0</br>");
            done=false;
            
        }
        if(Address==null || Address.isEmpty())
        {
            out.println("<br>Address cannot be blank</br>");
            done=false;
        }
        if(Gender==null || Gender.isEmpty())
        {
            out.println("<br>Sex cannot be blank</br>");
            done=false;
            
        }
        if(!(Gender.equals("M")||Gender.equals("F")) && Gender!=null && !Gender.isEmpty())
        {
            out.println("<br>You must enter Sex is F or M</br>");
            done=false;
        }
        if(Batch==null || Address.isEmpty())
        {
            out.println("<br>Batch cannot be blank</br>");
            done=false;
        }
        if(done==true)
        {
            Student st = new Student(id, name, Integer.parseInt(age), Address, Gender.charAt(0), Batch, Image);
            StudentDAO.AddStudent(st);
            response.sendRedirect("./listStudent.jsp");
            
            
            
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
